package com.qqtop10.manager.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.RebateLink;
import com.qqtop10.manager.model.temp.Goods;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.GoodsService;
import com.qqtop10.manager.service.RebateLinkService;

/**
 * 
 * @ClassName: RebateLinkController
 * @Description: 返利链接Controller
 * 
 */
@RequestMapping("/manager/rebatelink/")
@Controller
public class RebateLinkController extends BaseController {
	@Autowired
	private RebateLinkService rebateLinkService;

	@Autowired
	private GoodsService goodsService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(RebateLink input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			int count = rebateLinkService.count(input);
			List<RebateLink> array = rebateLinkService.all(input, pagingArgs);
			return page(count, array);
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("addRebate.do")
	@ResponseBody
	public Result AddRebateLink(RebateLink input) {
		String rebate_link = getParameter(input.getRebate_link());
		input.setRebate_link(rebate_link);
		input.setUuid(newId());
		input.setCreatetime(now());
		input.setCreator(getLogindUuid());
		if (input.getRebate_type() == 0) {
		/*	Goods goods = goodsService.selectGoodsByUUID(input.getRebate_productid());
			if (goods == null) {
				return err("商品不存在");
			}
			input.setRebate_product(goods.getName());
			input.setRebate_productid(goods.getUuid());*/
			input.setRebate_shoptype(3);
		}	
		int res = rebateLinkService.insert(input);
		if (res > 0) {
			return ok("操作成功");
		} else {
			return err("添加失败");
		}
	}

	@RequestMapping("updateRebate.do")
	@ResponseBody
	public Result UpdateRebateLink(RebateLink input) {
		String value = getParameter(input.getRebate_link());
		input.setRebate_link(value);
		input.setUpdatetime(now());
		input.setMender(getLogindUuid());
		if (input.getRebate_type() == 0) {
			
			input.setRebate_shoptype(3);
		}
		int ret = rebateLinkService.update(input);
		if (ret > 0) {
			return ok("更新成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("deleteById.do")
	@ResponseBody
	public Result DeleteById(int ids) {
		int ret = rebateLinkService.delete(ids);
		if (ret > 0) {
			return ok("删除成功");
		} else {
			return err("操作失败");
		}
	}

	@RequestMapping("getRebateLinkById.do")
	@ResponseBody
	public ModelMap GetRebateLinkById(int id) {
		ModelMap map = new ModelMap();
		RebateLink data = rebateLinkService.selectById(id);
		if (data == null) {
			map.put("data", "暂无信息");
		} else {
			map.put("data", data);
		}
		return map;
	}
}
